package com.zky.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.zky.gulimall.product.entity.AttrEntity;
import com.zky.gulimall.product.service.AttrAttrgroupRelationService;
import com.zky.gulimall.product.service.AttrService;
import com.zky.gulimall.product.service.CategoryService;
import com.zky.gulimall.product.vo.AttrGroupRelationVo;
import com.zky.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zky.gulimall.product.entity.AttrGroupEntity;
import com.zky.gulimall.product.service.AttrGroupService;
import com.zky.common.utils.PageUtils;
import com.zky.common.utils.R;



/**
 * 属性分组
 *
 * @author zky
 * @email 574096634@qq.com
 * @date 2022-08-17 21:28:48
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService relationService;

    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] relationVos){
        for (AttrGroupRelationVo relationVo : relationVos) {
            System.out.println("relationVos"+relationVo);
        }
      try{
          attrService.deleteRelation(Arrays.asList(relationVos));
          return R.ok();
      }catch (Exception e){
          e.printStackTrace();
          return R.error();
      }
    }

    /**
     * 关联属性分组
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
       List<AttrEntity> attrEntities=attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data", attrEntities);
    }

    /**
     * 新增属性关联关系
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> relationVos){
        relationService.saveBatch(relationVos);
        return R.ok();
    }


    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsVo> list=attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data",list);
    }

    /**
     * 未关联属性分组
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@RequestParam Map<String, Object> params,@PathVariable("attrgroupId") Long attrgroupId){
       PageUtils pageUtils=attrService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("page", pageUtils);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R listByCateLogId(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long cateLogId){
        PageUtils page = attrGroupService.queryPage(params,cateLogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        Long catelogId = attrGroup.getCatelogId();

       Long[] path= categoryService.findCategoryPath(catelogId);
       attrGroup.setCatelogPath(path);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
